﻿@implements IDisposable
@using System.ComponentModel
@using System.Reflection
@using System.Web

<div class="router-pagging-table">
    <Input @bind-Value="_url" OnPressEnter="() => _navigation.NavigateTo(_url, true)"/>
    <Table
        DataSource="@_forecasts"
        Total="_total"
        Loading="_loading"
        PageIndex="@_pageIndex"
        PageSize="@_pageSize"
        OnPageIndexChange="PageIndexChanged">
        <PropertyColumn Property="c=>c.Id"/>
        <PropertyColumn Property="c=>c.Date" Format="yyyy-MM-dd"/>
        <PropertyColumn Property="c=>c.TemperatureC"/>
        <PropertyColumn Title="Temp. (F)" Field="@context.TemperatureF"/>
        <PropertyColumn Property="c=>c.Summary"/>
    </Table>

    <br/>
    <p>PageIndex: @_pageIndex | PageSize: @_pageSize | Total: @_total</p>
</div>

@inject NavigationManager _navigation

@code {

    WeatherForecast[] _forecasts;

    int _pageIndex = 1;

    int _pageSize = 5;

    int _total = 0;

    string _url;

    bool _loading;

    protected override async Task OnInitializedAsync()
    {
        _url = _navigation.Uri;
        _total = 50;
        _navigation.LocationChanged += LocationChanged;
        LoadTableParameter();
        await LoadDataAsync();
    }

    private async void LocationChanged(object sender, LocationChangedEventArgs e)
    {
        _url = e.Location;
        LoadTableParameter();
        await LoadDataAsync();
    }

    private void LoadTableParameter()
    {
        var uri = _navigation.ToAbsoluteUri(_url);
        var query = HttpUtility.ParseQueryString(uri.Query);
        _pageIndex = int.TryParse(query["page"], out var index) ? index > 0 ? index : 1 : 1;
        _pageSize = int.TryParse(query["size"], out var size) ? size > 0 ? size : 5 : 5;
    }

    private void PageIndexChanged(PaginationEventArgs args)
    {
        _navigation.NavigateTo($"mock?type={GetType().ToString().Replace($"{Assembly.GetExecutingAssembly().GetName().Name}.", "")}&page={args.Page}&size={args.PageSize}");
    }

    private async Task LoadDataAsync()
    {
        _loading = true;
        StateHasChanged();
        _forecasts = await GetForecastAsync(_pageIndex, _pageSize);
        _loading = false;
        StateHasChanged();
    }

    public class WeatherForecast
    {
        public int Id { get; set; }

        [DisplayName("Date")]
        public DateTime Date { get; set; }

        [DisplayName("Temp. (C)")]
        public int TemperatureC { get; set; }

        [DisplayName("Summary")]
        public string Summary { get; set; }

        public int TemperatureF => 32 + (int)(TemperatureC / 0.5556);

        public bool Hot { get; set; }
    }

    private static readonly string[] Summaries = new[]
    {
        "Freezing", "Bracing", "Chilly", "Cool", "Mild", "Warm", "Balmy", "Hot", "Sweltering", "Scorching"
    };

    public async Task<WeatherForecast[]> GetForecastAsync(int pageIndex, int pageSize)
    {
        await Task.Delay(200);
        var rng = new Random();
        return await Task.FromResult(Enumerable.Range((pageIndex - 1) * pageSize + 1, pageSize).Select(index =>
        {
            var temperatureC = rng.Next(-20, 55);
            return new WeatherForecast
            {
                Id = index,
                Date = DateTime.Now.AddDays(index),
                TemperatureC = temperatureC,
                Summary = Summaries[rng.Next(Summaries.Length)],
                Hot = temperatureC > 30,
            };
        }).ToArray());
    }

    public void Dispose()
    {
        _navigation.LocationChanged -= LocationChanged;
    }

}

<style>
    .router-pagging-table {
        padding: 1em;
    }

        .router-pagging-table input {
            margin-bottom: 1em;
        }
</style>